package game;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.applet.*;

abstract class GameObject extends Applet
{
	private static final long serialVersionUID = 1L;
	private Image image;
	public Point2D position;
	public Point2D dimension;
	private Point2D origin;
	public Rectangle rectangle;
	protected float rotation = 0;
	private AffineTransform at = new AffineTransform();
	
	//Constructor
	public GameObject(int index, Point2D position, Point2D size)
	{
		this.image = LoadImage.GetInstance().textures.get(index);
		this.position  = position;
		this.dimension = size;
		this.origin = new Point2D.Double(dimension.getX() / 2, dimension.getY() / 2);
		this.transform();
		this.updateBoundingBox();
	}

	//Get/Set
	public Image getImage() {
		return image;
	}
	public void setImage(Image image) {
		this.image = image;
	}
	public Rectangle getRectangle() {
		return rectangle;
	}

	public void setRectangle(Rectangle rectangle) {
		this.rectangle = rectangle;
	}
	public Point2D getPosition() {
		return position;
	}
	public void setPosition(Point2D position) {
		this.position = position;
	}
	public void addPosition(Point2D position2) {
		this.position = new Point2D.Double(this.position.getX() + position2.getX(),
				this.position.getY() + position2.getY());
	}
	public Point2D getDimension() {
		return dimension;
	}
	public void setDimension(Point2D dimension) {
		this.dimension = dimension;
	}
	public float getRotation()
	{
		return rotation;
	}
	
	public Point2D getOrigin()
	{
		return this.origin;
	}
	public void setRotation(float rotation)
	{
		this.rotation = rotation;
	}
	
	public  void update()
	{
		this.updateBoundingBox();
	}

	public void draw(Graphics g)
	{
		Graphics2D g2d = (Graphics2D) g;
		transform();
		g2d.drawImage(image, at, this);
	}
	
	protected void transform()
	{
		at.setToIdentity();
		at.translate(this.position.getX(), this.position.getY());
		at.rotate(Math.toRadians(rotation), origin.getX(), origin.getY());
	}
	
	//Updating Rectangle
	private void updateBoundingBox()
	{
		this.rectangle = new Rectangle((int)this.position.getX(), (int)this.position.getY(),
							   		   (int)this.dimension.getX(), (int)this.dimension.getY());		
	}

	//Simple Collision Check
	public boolean collision(GameObject object)
	{
		return this.rectangle.intersects(object.rectangle);
	}
	public boolean collision(Rectangle rect)
	{
		return this.rectangle.intersects(rect);
	}
}
